import {createStore} from 'vuex'
import db, {PERMISSION_KEY, ROLE_KEY, TOKEN_KEY, USER_KEY} from "@/utils/db";

export default createStore({
  state: {
    user: db.getObj(USER_KEY),
    token: db.get(TOKEN_KEY),
    permissionList: db.getObj(PERMISSION_KEY),
    roleList: db.getObj(ROLE_KEY),
  },
  getters: {
    getToken(state) {
      return state.token;
    },
    getUser(state) {
      return state.user;
    },
    getPermissionList(state) {
      return state.permissionList;
    },
    getRoleList(state) {
      return state.roleList;
    },
  },
  mutations: {
    setToken(state, token) {
      state.token = token;
      db.save(TOKEN_KEY, token);
    },
    setUser(state, user) {
      state.user = user;
      db.saveObj(USER_KEY, user);
    },
    setPermissionList(state, permissionList) {
      state.permissionList = permissionList;
      db.saveObj(PERMISSION_KEY, permissionList);
    },
    setRoleList(state, roleList) {
      state.roleList = roleList;
      db.saveObj(ROLE_KEY, roleList);
    },
  },
  actions: {},
  modules: {}
})
